{% assign snapchat_id = storeConfig.snapchat_pixel_list | strtoarray %}
{% get_pixels_domain list={snapchat_id} %}
{%- assign snapchat_pixels = list -%}
{%- if snapchat_pixels -%}
<script type='text/javascript'>
    (function (e, t, n) {
        if (e.snaptr) return;
        var a = e.snaptr = function () {
            a.handleRequest ? a.handleRequest.apply(a, arguments) : a.queue.push(arguments)
        };
        a.queue = [];
        var s = 'script';
        r = t.createElement(s);
        r.async = !0;
        r.src = n;
        var u = t.getElementsByTagName(s)[0];
        u.parentNode.insertBefore(r, u);
    })(window, document,
        'https://sc-static.net/scevent.min.js');

    {%- for pixel in snapchat_pixels -%}
    snaptr('init', '{{ pixel.facebook_pixel_id }}', {'user_email': "{{pixel_user.email}}"});
    {%- endfor -%}

    {% if page_ca != 'order_checkout' %}
        snaptr('track', 'PAGE_VIEW', {description: '{{ template }}'});
    {% endif %}

    {%- if template == 'product_detail' -%}

    {%- if product.id and product.variant.sku_code -%}
    snaptr('track', 'VIEW_CONTENT', {
        'currency': '{{ base_currency.standard_code }}',
        'price': "{{ product.variant.price }}",
        'item_category': '{{product.product_type | filter_string_format}}',
        'number_items': 1,
        'item_ids': ['{{ product.spu | escape}}']
    });
    {%- endif -%}

    {%- elsif template == 'search_detail' and search.keywords -%}
    snaptr('track', 'SEARCH', {'search_string': '{{search.keywords | filter_string_format}}'});


    {%- elsif template == 'order_success' -%}
    var productList=[];
    {%- for product in order.product -%}
    productList.push("{{ product.spu | escape}}");
    {%- endfor -%}

    {% capture pixels %}snaptr_pixels_{{order.id}}{% endcapture %}
    {% get_cache key={pixels} var_name=snaptr_pixels_status%}
    {% if snaptr_pixels_status == 0 %}
    snaptr('track', 'PURCHASE', {
        'currency': '{{ order.currency_code }}',
        'item_ids': productList,
        'number_items': '{{ order.total_num }}',
        {%- if storeConfig.snapchat_sales_goods_amount -%}
        'price': "{{ order.current_subtotal_price| plus:order.current_coupon_price | plus:order.current_promotion_price }}",
        {%- else -%}
        'price': "{{ order.total_price }}",
        {%- endif -%}
        'transaction_id': '{{ order.order_number }}'
    });
    {% endif %}
    {%- elsif template == 'order_payment_gateway' and order.payment_type == 'offlinepay' and storeConfig.is_offlinepay_purchase_pixel == '1' -%}
        var productList=[];
        {%- for product in order.product -%}
        productList.push("{{ product.spu | escape}}");
        {%- endfor -%}
        {% capture pixels %}snaptr_pixels_{{order.id}}{% endcapture %}
        {% get_cache key={pixels} var_name=snaptr_pixels_status%}
        {% if snaptr_pixels_status == 0 %}
        snaptr('track', 'PURCHASE', {
            'currency': '{{ order.currency_code }}',
            'item_ids': productList,
            'number_items': '{{ order.total_num }}',
            {%- if storeConfig.snapchat_sales_goods_amount -%}
            'price': "{{ order.current_subtotal_price| plus:order.current_coupon_price | plus:order.current_promotion_price }}",
            {%- else -%}
            'price': "{{ order.total_price }}",
            {%- endif -%}
            'transaction_id': '{{ order.order_number }}'
        });
        {% endif %}
    {%- endif -%}
</script>
<script type='text/javascript'>
    window['TOP_GLOBAL_CALLBACK_LAYER'].register({
        add_to_cart:function(ecdata){
            var ids = [];
            ids.push(ecdata['ec_data_spu']);
            snaptr('track', 'ADD_CART', {
                'currency': '{{ base_currency.standard_code }}',
                'price': ecdata['ec_data_price'] * ecdata['ec_data_quantity'],
                'number_items': ecdata['ec_data_quantity'],
                'item_category': ecdata['ec_data_category'],
                'item_ids': ids
            });
        },
        remove_from_cart:function(ec_data){
            snaptr('track', 'remove_from_cart', {
                'currency': '{{ base_currency.standard_code }}',
                'price': ec_data['ec_data_price']*ec_data['ec_data_quantity'],
                'item_category': ec_data['ec_data_category'],
                'number_items': ec_data['ec_data_quantity'],
                'item_ids': ["ec_data['ec_data_spu']"]
            });
        },
        share_product:function(ec_data){
            snaptr('track', 'SHARE', {
            'currency': '{{ base_currency.standard_code }}',
            'price': ec_data['ec_data_price'],
            'item_category': ec_data['ec_data_category'],
            'number_items': ec_data['ec_data_quantity'],
            'item_ids': ec_data['ec_data_spu']
            });
        },
        begin_checkout:function(){
            snaptr('track', 'START_CHECKOUT');
        },
        generate_lead:function(){
            snaptr('track', 'SUBSCRIBE');
        },
        sign_up:function(){
            snaptr('track', 'SIGN_UP');
        },
        login(){
            snaptr('track', 'LOGIN');
        }
    })
</script>
{%- endif -%}

